<?php

namespace app\common\biz;

use app\common\enum\Code;

class Biz
{
    private static int $code = Code::Success->value;
    private static string $msg = '';
    private static mixed $data;


    public static function getMsg(): string
    {
        return self::$msg;
    }

    public static function getCode(): int
    {
        return self::$code;
    }

    public static function error(string $msg = Code::Fail->value, int $code = Code::Fail->value): static
    {
        self::$code = $code;
        self::$msg = $msg;
        return new static();
    }

    public static function errorByCode(Code $code): static
    {
        self::$code = $code->value;
        self::$msg = $code->msg();
        return new static();
    }

    public static function hasError(): bool
    {
        return !(Code::Success->value === self::$code);
    }

    public static function getError(): array
    {
        return [self::$msg, self::$code];
    }

    public static function success($data = null): static
    {
        self::$data = $data;
        return new static();
    }

    public static function getData()
    {
        return self::$data;
    }

    public static function setName(array &$dataList = [], array $kv = [], string $keyword = ''): void
    {
        foreach ($dataList as &$item) {
            if (isset($kv[$item[$keyword]])) {
                $item[$keyword . '_name'] = $kv[$item[$keyword]];
            } else {
                $item[$keyword . '_name'] = '';
            }
        }
    }
}